package cn.weezoo.jnademo;

import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.Platform;

/**
 * @author wenei
 * @date 2021-02-25 18:33
 */
public class JNATest {

    private static final String filePath;

    static {
        if (Platform.isWindows()) {
            System.out.println("Windows platform");
            // windows平台最前面的路径需要去掉一个/
            filePath = Thread.currentThread().getContextClassLoader().getResource("libhello.dll").getPath().substring(1);
        } else if (Platform.isLinux()) {
            System.out.println("Linux platform");
            filePath = Thread.currentThread().getContextClassLoader().getResource("libhello.so").getPath();
        } else {
            System.out.println("Other platform");
            filePath = null;
        }
    }

    interface CLibrary extends Library {


        CLibrary INSTANCE = (CLibrary) Native.loadLibrary(filePath, CLibrary.class);

        void hello();
    }

    public static void main(String[] args) {
        CLibrary instance = CLibrary.INSTANCE;
        instance.hello();
    }
}
